package com.rtwo.tio.common;

import org.tio.core.ChannelContext;
import org.tio.core.intf.Packet;
import org.tio.utils.json.Json;

import java.io.UnsupportedEncodingException;

/**
 * Created by hasee on 2018/7/5.
 */
public abstract class BasicHandler<T extends BasicBody> {

    public abstract Class<T> getClazz();

    public void handler(Packet packet,ChannelContext channelContext) throws UnsupportedEncodingException {
        BasicPacket basicPacket = (BasicPacket)packet;
        byte[] body = basicPacket.getBody();
        T t= Json.toBean(new String(body,"utf-8"),getClazz());
        postHanlder(t,channelContext);
    }

    public abstract void postHanlder(T body,ChannelContext channelContext) throws UnsupportedEncodingException;

}
